﻿<RadzenStack>
    <RadzenStack>
        <RadzenRow>
            <RadzenColumn>
                <RadzenButton Variant="Variant.Outlined" Click="SubmitHandle">保存</RadzenButton>
                <RadzenButton Variant="Variant.Outlined" Click="CancelHandle">取消</RadzenButton>
            </RadzenColumn>
        </RadzenRow>
    </RadzenStack>
    <RadzenFieldset AllowCollapse="true" Style="width: 100%; margin: 40px auto;" ExpandTitle="Expand orders." CollapseTitle="Collapse orders."
                    ExpandAriaLabel="Expand the list of orders." CollapseAriaLabel="Collapse the list of orders.">
        <HeaderTemplate>
            <span class="d-inline-flex align-items-center align-middle">
                <RadzenIcon Icon="account_box" class="me-1"/><b>基本信息</b>
            </span>
        </HeaderTemplate>
        <ChildContent>
            <RadzenStack class="rz-mb-2">
                <RadzenRow>
                    <RadzenColumn Size="12" SizeSM="6">
                        <RadzenFormField Text="物料编码" Style="width: 100%;">
                            <RadzenTextBox @bind-Value="_editRequestDto.Code"></RadzenTextBox>
                        </RadzenFormField>
                    </RadzenColumn>
                    <RadzenColumn Size="12" SizeSM="6">
                        <RadzenFormField Text="物料名称" Style="width: 100%;">
                            <RadzenTextBox @bind-Value="_editRequestDto.Name"></RadzenTextBox>
                        </RadzenFormField>
                    </RadzenColumn>
                    <RadzenColumn Size="12" SizeSM="6">
                        <RadzenFormField Text="规格型号" Style="width: 100%;">
                            <RadzenTextBox @bind-Value="_editRequestDto.Specification"></RadzenTextBox>
                        </RadzenFormField>
                    </RadzenColumn>
                    <RadzenColumn Size="12" SizeSM="6">
                        <RadzenFormField Text="单位" Style="width: 100%;">
                            <RadzenDropDown @bind-Value="_editRequestDto.Unit" LoadData="LoadDataHandler" Data="_units"
                                            TextProperty="Name" ValueProperty="Id" AllowFiltering="true">
                            </RadzenDropDown>
                        </RadzenFormField>
                    </RadzenColumn>
                    <RadzenColumn Size="12" SizeSM="6">
                        <RadzenFormField Text="物料分类" Style="width: 100%;">
                            <DropDownTree TValue="int" @bind-Value="_editRequestDto.MaterialType" Data="TreeData"
                                          TextProperty="Name" ChildrenProperty="Children" ValueProperty="Key"
                                          HasChildren="@(e => (e as MaterialCategoryTreeResponseDto)?.Children?.Any() ?? false)"
                                          GetItemFromTree="GetItemFromTreeHandler">
                            </DropDownTree>
                        </RadzenFormField>
                    </RadzenColumn>
                    <RadzenColumn Size="12" SizeSM="6">
                        <RadzenFormField Text="是否质检" Style="width: 100%;">

                        </RadzenFormField>
                    </RadzenColumn>
                    <RadzenColumn Size="12" SizeSM="6">
                        <RadzenFormField Text="是否安全库存" Style="width: 100%;">

                        </RadzenFormField>
                    </RadzenColumn>

                    <RadzenColumn Size="12">
                        <RadzenFormField Text="物料描述" Style="width: 100%;">
                            <RadzenTextArea @bind-Value="_editRequestDto.Description"></RadzenTextArea>
                        </RadzenFormField>
                    </RadzenColumn>
                </RadzenRow>
            </RadzenStack>
        </ChildContent>
    </RadzenFieldset>
</RadzenStack>


@code {

    async Task SubmitHandle()
    {
        var key = MaterialIndex?.GetKey();

        if (key != null && key > 0)
        {
            var result = await MaterialService.UpdateMaterial(key.Value, _editRequestDto);
        }
        else
        {
            var result = await MaterialService.CreateMaterial(_editRequestDto);
        }
    }

    void CancelHandle()
    {
        MaterialIndex?.SetKey(0);
        MaterialIndex?.SetDynamicComponentType(typeof(MaterialTable));
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        var unitResult = await CalculateUnitService.QueryCalculateUnits(new CalculateUnitSearchRequestDto());

        if (unitResult != null && unitResult.Succeeded)
        {
            _units = unitResult.Data;
        }

        var categoryTreeResult = await MaterialCategoryService.QueryMaterialCategoriesByTree();
        if (categoryTreeResult != null && categoryTreeResult.Succeeded)
        {
            TreeData = categoryTreeResult.Data!;
        }
    }

    async Task LoadDataHandler(LoadDataArgs args)
    {
        var result = await CalculateUnitService.QueryCalculateUnits(new CalculateUnitSearchRequestDto());

        if (result != null && result.Succeeded)
        {
            _units = result.Data;
        }

        InvokeAsync(StateHasChanged);
    }


    
}